import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { serviceApi } from '@/api/public/service'
import type { Service } from '@/types/service'
import { ElMessage } from 'element-plus'

export const useMyServices = () => {
  const router = useRouter()
  const userStore = useUserStore()
  const services = ref<Service[]>([])
  const loading = ref(false)

  const fetchMyServices = async () => {
    if (!userStore.isLoggedIn) {
      router.push('/login')
      return
    }

    loading.value = true
    try {
      const response = await serviceApi.getMyServices()
      if (response.data.code === 200) {
        services.value = response.data.data
      }
    } catch (error) {
      console.error('获取个人服务列表失败:', error)
      ElMessage.error('获取个人服务列表失败')
    } finally {
      loading.value = false
    }
  }

  onMounted(() => {
    fetchMyServices()
  })

  return {
    services,
    loading
  }
} 